package com.gitee.webflux.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;

/**
 * 开发人员   YooLin1c
 * 编写日期   2018/6/25
 * 联系邮箱   401664157@qq.com
 * 作用描述   webflux初体验
 */
@Slf4j
@RestController
public class WebFluxController {
    /**
     * 2018-06-25 20:10:45.044  INFO 39983 --- [ctor-http-nio-2] c.g.w.controller.WebFluxController       : mvc start
     * 2018-06-25 20:10:45.044  INFO 39983 --- [ctor-http-nio-2] c.g.w.controller.WebFluxController       : do something begin
     * 2018-06-25 20:10:50.050  INFO 39983 --- [ctor-http-nio-2] c.g.w.controller.WebFluxController       : do something end
     * 2018-06-25 20:10:50.050  INFO 39983 --- [ctor-http-nio-2] c.g.w.controller.WebFluxController       : mvc end
     *
     * @return
     */
    @GetMapping("/mvc")
    private String test1() {
        log.info("mvc start");
        String str = doSomething();
        log.info("mvc end");
        return str;
    }

    private String doSomething() {
        // 模拟延迟操作
        log.info("do something begin");
        try {
            TimeUnit.SECONDS.sleep(5);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        log.info("do something end");
        return "sth.";
    }

    /**
     * 2018-06-25 20:11:11.975  INFO 39983 --- [ctor-http-nio-2] c.g.w.controller.WebFluxController       : mvc start
     * 2018-06-25 20:11:11.976  INFO 39983 --- [ctor-http-nio-2] c.g.w.controller.WebFluxController       : mvc end
     * 2018-06-25 20:11:11.976  INFO 39983 --- [ctor-http-nio-2] c.g.w.controller.WebFluxController       : do something begin
     * 2018-06-25 20:11:16.980  INFO 39983 --- [ctor-http-nio-2] c.g.w.controller.WebFluxController       : do something end
     *
     * @return
     */
    @GetMapping("/mono")
    private Mono<String> test2() {
        log.info("mono start");
        // 惰性求值
        Mono<String> strMono = Mono.fromSupplier(this::doSomething);
        log.info("mono end");
        return strMono;
    }


    //    @GetMapping("/flux") // 一次性返回4条数据：flux data--1flux data--2flux data--3flux data--4
//    @GetMapping(value = "/flux", produces = "text/event-stream")
    /* 以流的形式返回数据:
        data:flux data--1
        data:flux data--2
        data:flux data--3
        data:flux data--4
    */
    @GetMapping(value = "/flux", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    private Flux<String> test3() {
        // 惰性求值
        Flux<String> strFlux = Flux.fromStream(IntStream.range(1, 5).mapToObj(i -> {
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "flux data--" + i;
        }));
        return strFlux;
    }
}